//
//  AppDelegate.m
//  BasketballClub
//
//  Created by 孙鸿吉 on 16/1/6.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "AppDelegate.h"
#import "NewsViewController.h"
#import "LiveViewController.h"
#import "SkillViewController.h"
#import "VideoViewController.h"
#import "OwnerViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "LiveHandle.h"
#import <UMSocial.h>
#import <UMSocialQQHandler.h>
#import <UMSocialWechatHandler.h>
#import <UMSocialSinaHandler.h>
#import "CoreDataManager.h"
#import <AFNetworking.h>
#import "TodayViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    //改变状态栏颜色
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
    //LeanCloud
    [AVOSCloud setApplicationId:@"vEmoIFO2cxOpTHVNRT04yTyC-gzGzoHsz" clientKey:@"TUuceJEGy9amx6sLJ0lejXRM"];
    
    //友盟
    [UMSocialData setAppKey:@"5693101667e58e370a00054f"];
    UITabBarController *tabBar = [[UITabBarController alloc]init];
    //qq
    [UMSocialQQHandler setQQWithAppId:@"1105091820" appKey:@"5vG7UMPPrh7Y9bhM" url:@"http://user.qzone.qq.com/395705277"];
    //微信,朋友圈
    [UMSocialWechatHandler setWXAppId:@"wx213e8540196366f1" appSecret:@"9a8049fdc9abb95177ec1c440fd3b4fe" url:@"http://user.qzone.qq.com/395705277"];
    //微博
    [UMSocialSinaHandler openSSOWithRedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
    //时讯
    NewsViewController *newsVC = [[NewsViewController alloc]init];
    
    UINavigationController *newsNC = [[UINavigationController alloc]initWithRootViewController:newsVC];
    //直播
    LiveViewController *liveVC = [[LiveViewController alloc]init];
    UINavigationController *liveNC = [[UINavigationController alloc]initWithRootViewController:liveVC];

    liveNC.navigationBar.hidden = YES;
    //技巧
    SkillViewController *skillVC = [[SkillViewController alloc]init];
    UINavigationController *skillNC = [[UINavigationController alloc]initWithRootViewController:skillVC];
    skillNC.navigationBar.hidden = YES;
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation"] forBarMetrics:UIBarMetricsDefault];
    [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tabbarBack"]];
    NSDictionary *colorDic = [NSDictionary dictionaryWithObject:[UIColor yellowColor] forKey:NSForegroundColorAttributeName];
    skillNC.navigationBar.titleTextAttributes = colorDic;
    //视频
    VideoViewController *videoVC = [[VideoViewController alloc]init];
    UINavigationController *videoNC = [[UINavigationController alloc]initWithRootViewController:videoVC];
    videoNC.navigationBar.hidden = YES;
    //我的
    OwnerViewController *ownerVC = [[OwnerViewController alloc]init];
    UINavigationController *ownerNC = [[UINavigationController alloc]initWithRootViewController:ownerVC];
    ownerNC.navigationBar.hidden = YES;
    tabBar.viewControllers = @[newsNC,liveNC,skillNC,videoNC,ownerNC];
    //设置tabBarItem图片
    newsNC.tabBarItem.image = [UIImage imageNamed:@"iconfont-lanqiu@2x"];
    liveNC.tabBarItem.image = [UIImage imageNamed:@"iconfont-632zhibojian (1)@2x"];
    skillNC.tabBarItem.image = [UIImage imageNamed:@"iconfont-jiqiaomiji@2x"];
    videoNC.tabBarItem.image = [UIImage imageNamed:@"iconfont-shipin@2x"];
    ownerNC.tabBarItem.image = [UIImage imageNamed:@"iconfont-wode@2x"];
    //设置tabBarItem标题
    newsNC.tabBarItem.title = @"时讯";
    liveNC.tabBarItem.title = @"赛程";
    skillNC.tabBarItem.title = @"技巧";
    videoNC.tabBarItem.title = @"视频";
    ownerNC.tabBarItem.title = @"我的";
    tabBar.tabBar.tintColor = [UIColor yellowColor];
    tabBar.tabBar.translucent = NO;
    self.window.rootViewController = tabBar;
    TodayViewController *todayVC = [[TodayViewController alloc]init];
    [[LiveHandle sharedLiveHandle] GetTodayDataFromNetWorkWithBlock:^(BOOL isFinished) {
        [todayVC.tableView reloadData];
    }];
    //创建coreData数据库
    [[CoreDataManager shareCoreDateManager] create];
    
    //无网络加载本地
    [self determineNetwork];
    [skillVC offLine];
    //无网络的时候弹出提示
    [newsVC offLine];
    
    return YES;
}




- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    BOOL result = [UMSocialSnsService handleOpenURL:url];
    if (result == FALSE) {
        
    }
    return result;
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
#pragma mark - 无网加载本地
-(void)determineNetwork

{
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        //如果当前网络状态为无网络状态
        if (status == AFNetworkReachabilityStatusNotReachable) {
            
            return ;
        }else{
            [[CoreDataManager shareCoreDateManager] deleteCBANewsAllData];
            [[CoreDataManager shareCoreDateManager] deleteNBANewsAllData];
            [[CoreDataManager shareCoreDateManager] deleteSkillAllData];
            [[CoreDataManager shareCoreDateManager] deleteVideoAllData];
            [[CoreDataManager shareCoreDateManager] deleteYesterdayAllData];
            [[CoreDataManager shareCoreDateManager] deleteTodayAllData];
            [[CoreDataManager shareCoreDateManager] deleteTomorrowAllData];
            
        }
        
    }];
    
}





@end
